<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use App\Models\Product;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ProductController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Product';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Product());

        $grid->column('id', __('Id'));
        $grid->column('category_id', '分类名')->display(function ($categoryId){
            $category = Category::find($categoryId);
            return empty($category)?'未知':$category->title;
        });
        $grid->column('name', '产品名');
        $grid->column('cover', __('Cover'))->display(function ($cover){
            return '<img src="'.$cover.'" style="width:55px;height:55px;">';
        });
        $grid->column('original_price', '原价');
        $grid->column('current_price', '优惠价');
        $grid->column('provider', '供应商');
        // $grid->column('created_at', '创建时间')->date('Y-m-d');
        $grid->column('created_at')->date('Y-m-d');
        // $grid->column('updated_at', __('Updated at'));
        // $grid->column('attributes', __('Attributes'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Product::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('category_id', __('Category id'));
        $show->field('name', __('Name'));
        $show->field('cover', __('Cover'));
        $show->field('original_price', __('Original price'));
        $show->field('current_price', __('Current price'));
        $show->field('summary', __('Summary'));
        $show->field('description', __('Description'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('provider', __('Provider'));
        $show->field('attributes', __('Attributes'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Product());

        $form->number('category_id', __('Category id'));
        $form->text('name', __('Name'));
        $form->image('cover', __('Cover'));
        $form->decimal('original_price', __('Original price'))->default(0.00);
        $form->decimal('current_price', __('Current price'))->default(0.00);
        $form->text('summary', __('Summary'));
        $form->textarea('description', __('Description'));
        $form->text('provider', __('Provider'));
        $form->text('attributes', __('Attributes'));

        return $form;
    }
}
